<template>
	<view class="content wrap">
		<u-form :model="form" ref="uForm"  :rules="rules">
			<u-form-item label="单位名称" label-width="160" left-icon="home" prop="getUnit">
				<u-input v-model="form.getUnit" placeholder="请输入名称"/>
				<u-icon @click="getg()" slot="right" name="search" size="60"></u-icon>
			</u-form-item>
			
			<u-form-item label="姓名" label-width="150" left-icon="account" prop="name">
				<u-input v-model="form.name" placeholder="请输入姓名"/>
			</u-form-item>
			
			<u-form-item label="手机号" label-width="150" left-icon="phone" prop="account">
				<u-input v-model="form.account" type="digit" placeholder="请输入手机号/账号"/>
			</u-form-item>
			
			<u-form-item label="默认密码" label-width="150" prop="password">
				<u-input v-model="form.password" placeholder="请输入密码"/>
			</u-form-item>
			
			<u-form-item label="性别" label-width="150">
				<u-radio-group v-model="form.sex">
					<u-radio 
						@change="radioChange3" 
						v-for="(item, index) in sixlist" :key="index" 
						:name="item.name"
					>
					{{item.name}}
					</u-radio>
				</u-radio-group>
			</u-form-item>
		</u-form>
		<u-button @click="submit">提交</u-button>
		<u-popup v-model="show" mode="center" closeable width="550rpx" height="550rpx" border-radius="10">
			<view class=" u-p-r-30 u-p-l-30 u-m-t-20" style="margin-top: 40rpx;">
				<u-search placeholder="请输入关键词搜索" :clearabled="true" v-model="keyword" border-color="#606266" @custom="search" @search="search"></u-search>
			</view>
			<view class="wrap">
				<u-row v-for="(item,index) in glist" :key="index" @click="xuanzeg(item)">
					<u-col span="12">
						<view class="title u-m-t-20">
							<span>{{item.putgoodsName}}</span>
						</view>
					</u-col>
				</u-row>
			</view>
		</u-popup>
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				keyword:"",
				form:{
					name:"",
					account:"",
					password:"111111",
					sex:"男",
					operateId:'',
					getUnitId:"",
					getUnit:"",
				},
				sixlist: [
					{
						name: '男'
					},
					{
						name: '女'
					}
				],
			    rules: {
				},
				show:false,
				glist:[],
			};
		},
		onLoad:function(open){
			//获取司机扫码的项目id
			try{
				var params = this.getQueryParams(open.q)
				if(typeof  params.operateId != 'undefined' && params.operateId != '' && params.operateId != null){
					this.form.operateId= params.operateId
				} 
			} catch (e){
				let getStorageSyncOperateId = uni.getStorageSync('operateId')
				if(typeof getStorageSyncOperateId != 'undefined' && getStorageSyncOperateId != '' && getStorageSyncOperateId != null){
					this.form.operateId= getStorageSyncOperateId
				} 
			}
		},
		methods:{
			getQueryParams(queryString) {
				// 微信扫码得到的内容进行了一次编码，所以官方要求需要进行decodeURIComponent一次
				queryString = decodeURIComponent(queryString)
				let params = {}
				if (queryString) {
				  let queryArray = queryString.split('?')
				  if (queryArray.length > 1) {
					let query = queryArray[1]
					let array = query.split('&')
					array.map((value) => {
					  let valueArray = value.split('=') 
					  if (valueArray.length > 1) {
						// 还需要对value进行解码（可能涉及到在value为中文字符，在赋值到二维码前进行encodeURI编码）
						Object.assign(params, { [valueArray[0]]: decodeURI(valueArray[1]) })
					  }
					})
				  }
				}
				return params
			},
			// 选中某个单选框时，由radio时触发
			radioChange3(e) {
				console.log(e);
			},
			xuanzeg(item){
				this.form.getUnit = item.putgoodsName
				this.form.getUnitId = item.id
				this.show = false
			},
			search(){
				uni.showLoading({
					title: '加载中'
				})
				this.$http.post('/rest/driverAndCar/findbasicPutgoods5',{
				},{
					params: {
						putgoodsName:this.keyword,
						operateId:this.form.operateId,
					},
					//anth 是否验证，非必填， false: 不需要验证 true: 需要验证,默认为true需要验证.
					custom: {auth: false},
					header: {
						'content-type': 'application/x-www-form-urlencoded',
					}
				}).then(res => {
					uni.hideLoading()
					this.glist = res.data.data
				})
			},
			getg(){
				uni.showLoading({
					title: '加载中'
				})
				this.$http.post('/rest/driverAndCar/findbasicPutgoods5',{
				},{
					params: {
						putgoodsName:this.keyword,
						operateId:this.form.operateId,
					},
					//anth 是否验证，非必填， false: 不需要验证 true: 需要验证,默认为true需要验证.
					custom: {auth: false},
					header: {
						'content-type': 'application/x-www-form-urlencoded',
					}
				}).then(res => {
					uni.hideLoading()
					this.glist = res.data.data
					this.show = true
				})
			},
			submit() {
				if(typeof this.form.getUnit =='undefined'|| this.form.getUnit == null || this.form.getUnit == ''){
					uni.showToast({
						title: '请输入单位名称',
						duration: 2000,
						icon:'none',
					});
					return false;
				}
				if(typeof this.form.name =='undefined'|| this.form.name == null || this.form.name == ''){
					uni.showToast({
						title: '请输入姓名',
						duration: 2000,
						icon:'none',
					});
					return false;
				}
				if(typeof this.form.account =='undefined'|| this.form.account == null || this.form.account == ''){
					uni.showToast({
						title: '请输入手机号码',
						duration: 2000,
						icon:'none',
					});
					return false;
				}
				if(typeof this.form.password =='undefined'|| this.form.password == null || this.form.password == ''){
					uni.showToast({
						title: '请输入密码',
						duration: 2000,
						icon:'none',
					});
					return false;
				}
				uni.showLoading({
					title: '正在提交'
				})
				this.$http.post('/rest/driverAndCar/registerUnit',{
				},{
					params: this.form,
					//anth 是否验证，非必填， false: 不需要验证 true: 需要验证,默认为true需要验证.
					custom: {auth: false},
					header: {
						'content-type': 'application/x-www-form-urlencoded',
					}
				}).then(res => {
					uni.hideLoading()
					if(res.data.code == 200) {
						uni.showToast({
							title: '注册成功，正在登录',
							duration: 2000
						});
						this.$http.post('/rest/login',{
						},{
							params: {
								username: this.form.account,
								password: this.form.password,
							},
							custom: {auth: false},
							header: {
								'content-type': 'application/x-www-form-urlencoded',
							}
						}).then(res => {
							let data = res.data.data
							if(res.data.code != 200) {
								uni.showToast({
									title: res.data.message,
									duration: 2000,
									icon:'none',
								});
							} else {
								try {
									uni.setStorageSync('token', data);
									setTimeout(() => {
										this.$Router.pushTab({ 
											name: 'index',
										})
									}, 1000)
								} catch (e) {
									uni.showToast({
										title: '登录失败:'+e,
										duration: 2000,
										icon:'none',
									});
								}
							}
						}).catch(err => {
							uni.showToast({
								title: err.data.message,
								duration: 2000,
								icon:'none',
							});
						});
					} else {
						uni.showToast({
							title: res.data.message,
							duration: 2000,
							icon:'none',
						});
					}
				}).catch(err => {
					uni.showToast({
						title: err.data.message,
						duration: 2000,
						icon:'none',
					});
				});
			},
		}
	}
</script>

<style lang="scss">
	page{
			background-color: #ffffff;
	}
	
	.wrap {
		padding: 30rpx;
	}
</style>
